import * as url from 'url';
import * as path from 'path';
import config from 'config';
import { Env } from '../interfaces/global.interface';

export function isEnv(...args: Env[]): boolean {
  const env = process.env.NODE_ENV || 'development';
  return args.includes(env as Env);
}

// production -> prod, test -> test
export function getShortEnv(): string {
  if (isEnv('production')) {
    return 'prod';
  }
  if (isEnv('development')) {
    return 'alpha';
  }
  return process.env.NODE_ENV;
}

export function getVersion(): string {
  return process.env.VERSION || 'alpha';
}

export function isFullUrl(url: string) {
  return /^https?:|^\/\//.test(url);
}

export function isMicrofe(cdnRoot: string) {
  return cdnRoot.startsWith('micro-fe');
}


export function createIndexUrl(cdnRoot: string): string {
  // const internal = isEnv('production', 'demo') ? '-internal' : '';
  const cdnRootTrim = cdnRoot.replace(/^\/|\/$/, '');
  let envPath = `${getShortEnv()}/`;
  if (cdnRootTrim.startsWith('micro-fe') && envPath === 'prod/') {
    envPath = '';
  }
  return `${config.get('OSS_BASE_URL')}${cdnRootTrim}/${envPath}index.html`;
}

export function createEnvPath() {
  return `www/${getVersion()}/`;
}
export function createVersionPath(cdnRoot: string) {
  const cdnRootTrim = cdnRoot.replace(/^\/|\/$/, '');
  const versionDir = 'www/';
  const [headDir, tailDir] = cdnRootTrim.split(versionDir);
  return `${versionDir}${getVersion()}/${headDir}${tailDir}`;
}

export function createVersionIndexUrl(cdnRoot: string): string {
  const versionPath = createVersionPath(cdnRoot);
  return `${config.get('OSS_BASE_URL')}${versionPath}/index.html`;
}

export function joinUrlPath(baseUrl: string, ...paths: string[]): string {
  const urlObj = url.parse(baseUrl);
  urlObj.pathname = path.posix.join(urlObj.pathname, ...paths);
  const searchIdx = urlObj.pathname.indexOf('?');
  if (searchIdx > -1) {
    urlObj.search = urlObj.pathname.slice(searchIdx + 1);
    urlObj.pathname = urlObj.pathname.slice(0, searchIdx);
  }
  return url.format(urlObj);
}

/* 微前端路径拼接 */
export function joinMicrofeUrlPath(
  baseUrl: string,
  cdnRoot: string,
  env: string,
): string {
  const urlObj = url.parse(baseUrl);
  if (env === 'prod') {
    urlObj.pathname = path.posix.join(urlObj.pathname, cdnRoot, '/');
  } else {
    urlObj.pathname = path.posix.join(urlObj.pathname, cdnRoot, env, '/');
  }
  return url.format(urlObj);
}

export function joinImportmap(
  baseUrl: string,
  cdnRoot: string,
  env: string,
): string {
  const urlObj = url.parse(baseUrl);
  cdnRoot = cdnRoot
    .split('/')
    .slice(0, -1)
    .join('/');
  if (env === 'prod') {
    urlObj.pathname = path.posix.join(urlObj.pathname, cdnRoot, 'importmap.js');
  } else {
    urlObj.pathname = path.posix.join(
      urlObj.pathname,
      cdnRoot,
      `importmap-${env}.js`,
    );
  }
  return url.format(urlObj);
}

export function joinVersionImportmap(baseUrl: string, cdnRoot: string): string {
  const urlObj = url.parse(baseUrl);
  cdnRoot = createVersionPath(cdnRoot)
    .split('/')
    .slice(0, -1)
    .join('/');
  urlObj.pathname = path.posix.join(urlObj.pathname, cdnRoot, `importmap.js`);
  return url.format(urlObj);
}
